package com.atguigu;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test111 {

	public static void main(String[] args) {
		
		final Num n = new Num();
		final Lock l = new ReentrantLock();
		new Thread( new Runnable() {
			public void run() {
				
				n.AddNum();
			}
		},"AA").start();
		
		new Thread( new Runnable() {
			public void run() {
				n.DelNum();
			}
		},"BB").start();
	}
	
	
}

class Num{
	
	private int num = 0;
	Lock l = new ReentrantLock();
	//增加数量
	public void AddNum(){
		l.lock();
		notify();
		try {
			for (int i = 0; i < 10; i++) {
				
				++ num;
				System.out.println(num);
				wait();
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			l.unlock();
			
		}
		
		
	}
	
	
	
	//减少数量
	public void DelNum(){
		l.lock();
		notify();
		try {
			for (int i = 0; i < 10; i++) {
				
				-- num;
				System.out.println(num);
				wait();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			l.unlock();
		}
		
	}
	
}
